package com.wg.mycat.separation.controller;

import com.wg.mycat.common.ResultEntity;
import com.wg.mycat.separation.domain.MycatInfo;
import com.wg.mycat.separation.service.IMycatInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping({"/separation/"})
public class MycatInfoController {

    private static Logger logger = LoggerFactory.getLogger(MycatInfoController.class);

    @Autowired
    IMycatInfoService mycatInfoService;

    @RequestMapping(value = "get/mycatinfo", method = RequestMethod.GET)
    public ResponseEntity<?> getMycatInfoList() {
        logger.info("access getMycatInfoList interface success!");
        List<MycatInfo> mycatInfoList = mycatInfoService.getMycatInfoList();
        if(mycatInfoList.size()>0){
            return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.OK, mycatInfoList,"查询 MycatInfoList 成功！"));
        }
        mycatInfoList = new ArrayList<>();
        return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.FAILURE, mycatInfoList,"查询 MycatInfoList 失败！"));
    }

    @RequestMapping(value = "update/mycatinfo/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> updateMycatInfo(@PathVariable("id") String id) {
        int mycatInfo = mycatInfoService.updateMycatInfo(id);
        if(1 ==mycatInfo){
            return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.OK, mycatInfo,"更新 MycatInfo 成功！"));
        }
        mycatInfo = 0;
        return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.FAILURE, mycatInfo,"更新 MycatInfo 失败！"));
    }

    @RequestMapping(value = "insert/mycatinfo/{age}", method = RequestMethod.GET)
    public ResponseEntity<?> insertMycatInfo(@PathVariable("age") int age) {
        int mycatInfo = mycatInfoService.insertMycatInfo(age);
        if(1==mycatInfo){
            return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.OK, mycatInfo,"插入 MycatInfo 成功！"));
        }
        mycatInfo = 0;
        return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.FAILURE, mycatInfo,"插入 MycatInfo 失败！"));
    }

    @RequestMapping(value = "update/get/mycatinfo/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> updateFromMycatInfo(@PathVariable("id") String id) {
        MycatInfo selectMycatInfo = mycatInfoService.getMycatInfoById(id);
        MycatInfo mycatInfo = mycatInfoService.updateFromMycatInfo(id);
        if(null!=mycatInfo){
            return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.OK, mycatInfo,"更新 MycatInfo 成功！"));
        }
        mycatInfo = new MycatInfo();
        return ResponseEntity.ok().body(new ResultEntity(ResultEntity.ResultStatus.FAILURE, mycatInfo,"更新 MycatInfo 失败！"));
    }

}
